﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using System.Reflection;
using Portal.Workflow.Model;
using Portal.Workflow.StartPluginTemplate;
using Portal.Core;
using System.Data.SqlClient;
using System.Data;

namespace Portal.Workflow.Utilities
{
    public class WorkflowStartPluginMan
    {
        public static string ServiceId
        {
            get
            {
                return "WorkflowStartPlugin";
            }
        }


        public static IWorkflowStartPlugin[] LoadAssembly(
            string filename)
        {
            ArrayList ArrayListModules = new ArrayList();
            Assembly a = Assembly.LoadFrom(filename);
            string Msg = "Assembly not found or not valid";
            if (a == null) throw new Exception(Msg);
            foreach (Module mod in a.GetLoadedModules())
            {
                foreach (Type ty in mod.GetTypes())
                {
                    foreach (Type intf in ty.GetInterfaces())
                    {
                        if (intf == typeof(IWorkflowStartPlugin))
                        {
                            try
                            {
                                IWorkflowStartPlugin w = (IWorkflowStartPlugin)a.CreateInstance(ty.FullName);
                                if (w.ServiceId != null)
                                {
                                    ArrayListModules.Add(w);
                                }
                            }
                            catch (System.Exception ex)
                            {
                                ex.Source = "";
                            }
                        }
                    }
                }
            }
            IWorkflowStartPlugin[] ws = new IWorkflowStartPlugin[ArrayListModules.Count];
            for (int iIndex = 0; iIndex < ArrayListModules.Count; iIndex++)
            {
                ws[iIndex] = (IWorkflowStartPlugin)ArrayListModules[iIndex];
            }
            return ws;
        }


        public static IWorkflowStartPlugin Find(string DllPath, string WfFunctionId)
        {
            WfFunctionId = WfFunctionId.Trim();
            IWorkflowStartPlugin[] ItemLoadWfFunctions = LoadWfFunctions(DllPath);
            for (int iIndex = 0; iIndex < ItemLoadWfFunctions.Length; iIndex++)
            {
                if (ItemLoadWfFunctions[iIndex].ServiceId.Trim().Equals(WfFunctionId.Trim()))
                {
                    return ItemLoadWfFunctions[iIndex];
                }
            }
            return null;
        }

        public static void ExecuteStartPluginMan(
            IDbTransaction Trans, 
            SiteParam OSiteParam,
            string ObjectType,
            string ObjectId, 
            object odata,
            string CommandAction)
        {
            string DllPath=OSiteParam.DllPath;
            IWorkflowStartPlugin[] ItemLoadWfFunctions = LoadWfFunctions(DllPath);
            for (int iIndex = 0; iIndex < ItemLoadWfFunctions.Length; iIndex++)
            {
                ItemLoadWfFunctions[iIndex].Execute(Trans, OSiteParam.SiteId, ObjectType, ObjectId, odata, CommandAction);
            }
        }


        public static IWorkflowStartPlugin[] LoadWfFunctions(string DllPath)
        {
            string[] Files = System.IO.Directory.GetFiles(DllPath, "*.dll");
            ArrayList ArrayListServives = new ArrayList();
            for (int iIndex = 0; iIndex < Files.Length; iIndex++)
            {
                try
                {
                    string FileDll = Files[iIndex];
                    IWorkflowStartPlugin[]
                        asms = LoadAssembly(FileDll);
                    for (int jIndex = 0; jIndex < asms.Length; jIndex++)
                    {
                        ArrayListServives.Add(asms[jIndex]);
                    }
                }
                catch (System.Exception ex)
                {
                    ex.Source = "";
                }
            }
            IWorkflowStartPlugin[] WfFunctions = new IWorkflowStartPlugin[ArrayListServives.Count];
            for (int iIndex = 0; iIndex < ArrayListServives.Count; iIndex++)
            {
                WfFunctions[iIndex] = (IWorkflowStartPlugin)ArrayListServives[iIndex];
            }
            return WfFunctions;
        }
    }
}
